(define (square x) (* x x))

(define (even? x) (= (remainder x 2) 0))

(define (smallest-divisor n)
   (find-divisor n 2))

(define (find-divisor n test-divisor)
  (cond ((> (square test-divisor) n) n)
        ((divides? test-divisor n) test-divisor)
        (else (find-divisor n (+ test-divisor 1)))))

(define (divides? a b)
  (= (remainder b a) 0))

(define (prime? n)
   (= n (smallest-divisor n)))

(define runtime current-milliseconds)

(define (timed-prime-test n)
   (newline)
   (display n)
   (start-prime-test n (runtime)))

(define (start-prime-test n start-time)
  (if (prime? n)
      (report-prime (- (runtime) start-time))))

(define (report-prime elapsed-time)
  (display " *** ")
  (display elapsed-time))

(define (search-for-primes start stop)
  (cond ((even? start) (search-for-primes-helper (+ start 1) stop))
        (else (search-for-primes-helper start  stop))))


(define (search-for-primes-helper candidate stop)
  ;(display "helper ")
  ;(display candidate)
  ;(display "; ")
  ;(display stop)
  ;(newline)
  (cond ((< candidate stop) (timed-prime-test candidate)))
;                          (search-for-primes (+ candidate 2) stop))))